package com.ly.hotel.spainterface.jxd.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jg.view.CustomToast;
import com.ly.hotel.spainterface.jxd.activity.JxdJSPopupWindow;
import com.ly.hotel.spainterface.jxd.adapter.JxdJSItemAdapter;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.jxd.bean.Massage;
import com.ly.hotel.spainterface.jxd.bean.PluginBean;
import com.ly.hotel.spainterface.jxd.bean.Therapy;
import com.ly.hotel.spainterface.jxd.event.JxdCloseJSDetailDialogEvent;
import com.ly.hotel.spainterface.jxd.utils.AvoidDoubleClickedUtils;
import com.ly.hotel.spainterface.jxd.utils.TimeUtils;
import com.ly.hotel.util.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by ThinkPad on 2018/3/28.
 */

public class JxdJSPopupTherapyDetailsDialog extends Dialog {

    private TextView therapyNo;
    private TextView therapyName;
    private TextView therapyStatus;
    private TextView therapyDownTimes;
    private ImageButton therapyDetailsClose;
    private TextView therapySex;
    private TextView therapyAge;
    private TextView therapyHome;
    private TextView therapyHeight;
    private GridView therapyItemGridView;
    private ImageView detailsTherapyIamge;
    private TextView noHallCannotArrange;
    private Button therapyArrange;
    private List<Therapy> therapies;
    private String isHall;
    private PluginBean pluginBean;
    private MachineBean machineBean;
    private JxdJSPopupWindow mContext;
    private int rod = 0;
    private Therapy therapy;
    private Massage massage;
    private String arrangeTimeOut;
    private long lastArrangeTime = 0;
    private long nowTime = 0;
    private long timeOut = 0;
    private final int GET_MY_SPACE_DATA = 7;// 获取保存到服务器的安排时间

    public JxdJSPopupTherapyDetailsDialog(JxdJSPopupWindow context, int theme) {
        super(context, theme);
        mContext = context;
        rod = mContext.getResources().getDimensionPixelSize(R.dimen.px6);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        try{
            timeOut = Long.parseLong(pluginBean.getArrangeTimeOut());
        }catch (Exception e){}
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    // 安排成功，事件返回
    public void onEventMainThread(JxdCloseJSDetailDialogEvent event){
        dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jxd_pop_therapy_details_layout);
        initView();
        closeDialog();
        setTherapyData();
        setSelectItem();
        setArrange();
    }

    public void initView(){
        therapyNo = findViewById(R.id.therapy_no);
        therapyName = findViewById(R.id.therapy_name);
        therapyStatus = findViewById(R.id.therapy_status);
        therapyDownTimes = findViewById(R.id.therapy_downtime);
        therapyDetailsClose = findViewById(R.id.therapy_dialog_close);
        therapySex = findViewById(R.id.therapy_sex);
        therapyAge = findViewById(R.id.therapy_age);
        therapyHome = findViewById(R.id.therapy_home);
        therapyHeight = findViewById(R.id.therapy_height);
        therapyItemGridView = findViewById(R.id.therapy_item_gridview);
        detailsTherapyIamge = findViewById(R.id.details_therapy_iamge);
        noHallCannotArrange = findViewById(R.id.no_hall_cannot_arrange);
        therapyArrange = findViewById(R.id.therapy_arrange);

        // 全身项目隐藏安排按钮
        if(isHall.equals("否")){
            therapyArrange.setVisibility(View.GONE);
            noHallCannotArrange.setText(mContext.getResources().getString(R.string.can_not_arrange_tips));
            noHallCannotArrange.setVisibility(View.VISIBLE);
        }

        // 根据配置，决定是否只能浏览，不能安排
        if(pluginBean != null){
            if(pluginBean.isWaiterId()){
                therapyArrange.setVisibility(View.GONE);
                noHallCannotArrange.setText(mContext.getResources().getString(R.string.only_see_tips));
                noHallCannotArrange.setVisibility(View.VISIBLE);
                }
        }
    }

    // 关闭dialog
    public void closeDialog(){
        therapyDetailsClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setArrange() {
        therapyArrange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AvoidDoubleClickedUtils.isNotDoubleClicked()){
                    return;
                }
                nowTime = TimeUtils.getSystemMills();
                if(therapy.getStatus().equals("空闲") && therapy.getOnduty().equals("当值")){
                    Log.i("Jxd",nowTime + "-" + timeOut + "-" + lastArrangeTime);
                    if(timeOut > 0 && (nowTime - lastArrangeTime) < timeOut){
                        int minute = (int) (timeOut / (1000 * 60));
                        CustomToast customToast = CustomToast.makeText(mContext,mContext.getResources().getString(R.string.time_out) + minute + mContext.getResources().getString(R.string.try_again),Toast.LENGTH_SHORT,false);
                        customToast.setGravity(Gravity.CENTER,0,0);
                        customToast.show();
                    }else{
                        JxdPopArrangeDialog arrangeDialog = new JxdPopArrangeDialog(mContext,R.style.introDialog);
                        arrangeDialog.setCanceledOnTouchOutside(true);
                        arrangeDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
                        arrangeDialog.setPluginBean(pluginBean);
                        arrangeDialog.setMachineBean(machineBean);
                        arrangeDialog.setIsSpec("");
                        arrangeDialog.setArtNo(therapy.getArtNo());
                        arrangeDialog.setMassageName(massage.getMassagename());
                        arrangeDialog.show();
                    }
                }else{
                    CustomToast customToast = CustomToast.makeText(mContext,mContext.getResources().getString(R.string.no_free_tips), Toast.LENGTH_SHORT,false);
                    customToast.setGravity(Gravity.CENTER,0,0);
                    customToast.show();
                }
            }
        });
    }

    public void setTherapyData(){
        therapy = therapies.get(0);
        String imageUrl = "";
        // 根据imgSrc配置是否使用收银系统的技师图片
        if (pluginBean != null) {
            if (!TextUtils.isEmpty(pluginBean.getImgSrc())){
                imageUrl = pluginBean.getImgSrc() + therapy.getArtNo() + ".jpg";
            }else {
                imageUrl = MyApplication.getHostResUrl() + "/upload/interface/technician/pic/" + therapy.getArtNo() + ".jpg";
            }
        }else {
            imageUrl = MyApplication.getHostResUrl() + "/upload/interface/technician/pic/" + therapy.getArtNo() + ".jpg";
        }
        ImageUtil.displayImage_Round(mContext,imageUrl,detailsTherapyIamge,rod);
        try {
            if(therapy.getArtNo() != null){
                therapyNo.setText(Html.fromHtml(mContext.getResources().getString(R.string.technician_id) + "<font color='#00DFFF'>" + therapy.getArtNo() + "</font>"));
            }
        }catch (Exception e){}

        try {
            if(therapy.getArtName() != null){
                if(pluginBean.isShowName()){
                    therapyName.setText(mContext.getResources().getString(R.string.technician_name) + therapy.getArtName());
                }
                if(pluginBean.isNameFiltrate()){
                    if(therapy.getArtName().contains("红牌")){
                        therapyName.setText(mContext.getResources().getString(R.string.technician_name) + "红牌");
                    }
                }
            }
        }catch (Exception e){}

        try {
            if(therapy.getStatus().equals("空闲")){
                if(therapy.getOnduty().equals("当值")){
                    therapyStatus.setText(Html.fromHtml(mContext.getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getStatus() + "</font>"));
                }else{
                    therapyStatus.setText(Html.fromHtml(mContext.getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getOnduty() + "</font>"));
                }
            }else{
                therapyStatus.setText(Html.fromHtml(mContext.getResources().getString(R.string.technician_status) + "<font color='#00DFFF'>" + therapy.getStatus() + "</font>"));
            }
        }catch (Exception e){}

        try {
            if(therapy.getDownMinute() != null){
                if(pluginBean.isShowEndTime() && therapy.getStatus().equals("在做钟")){
                    therapyDownTimes.setText(mContext.getResources().getString(R.string.down_time) + therapy.getDownMinute() + mContext.getResources().getString(R.string.minute));
                }
            }
        }catch (Exception e){}

        try {
            if(therapy.getSex() != null){
                therapySex.setText(mContext.getResources().getString(R.string.sex) + therapy.getSex());
            }
        }catch (Exception e){}

        try {
            if(therapy.getAge() != null){
                therapyAge.setText(mContext.getResources().getString(R.string.age) + therapy.getAge());
            }
        }catch (Exception e){}

        try {
            if(therapy.getHeight() != null){
                therapyHeight.setText(mContext.getResources().getString(R.string.height) + therapy.getHeight());
            }
        }catch (Exception e){}

        try {
            if(therapy.getHome() != null){
                therapyHome.setText(mContext.getResources().getString(R.string.home) + therapy.getHome());
            }
        }catch (Exception e){}
    }

    public void setSelectItem(){
        // 存储项目信息
        final List<Massage> massages = new ArrayList<>();
        for(int i = 0;i < therapies.size();i ++){
            Therapy therapyInfo = therapies.get(i);
            Massage massage = new Massage();
            massage.setMassagename(therapyInfo.getArtModeNo());
            massages.add(massage);
        }

        final JxdJSItemAdapter itemAdapter = new JxdJSItemAdapter(mContext,massages);
        therapyItemGridView.setAdapter(itemAdapter);
        therapyItemGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemAdapter.setDefaultPositon(position);
                massage = massages.get(position);
                itemAdapter.notifyDataSetChanged();
            }
        });
        therapyItemGridView.performItemClick(therapyItemGridView.getChildAt(0),0,therapyItemGridView.getItemIdAtPosition(0));
    }

    public void setTherapies(List<Therapy> therapies) {
        this.therapies = therapies;
    }

    public void setIsHall(String isHall) {
        this.isHall = isHall;
    }

    public void setPluginBean(PluginBean pluginBean) {
        this.pluginBean = pluginBean;
    }

    public void setMachineBean(MachineBean machineBean) {
        this.machineBean = machineBean;
    }

    public void setArrangeTimeOut(String arrangeTimeOut) {
        this.arrangeTimeOut = arrangeTimeOut;
    }


}
